package com.gsbelarus.gedemin.salary.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;

import com.gsbelarus.gedemin.lib.sync.protocol.entity.SyncServiceStatus;
import com.gsbelarus.gedemin.lib.ui.activity.SyncActivityModel;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.entity.model.GdMsgModel;
import com.gsbelarus.gedemin.salary.fragment.gdmsg.GdMsgDetailPagerFragment;
import com.gsbelarus.gedemin.salary.fragment.gdmsg.GdMsgsListFragment;
import com.gsbelarus.gedemin.salary.util.GCMHelper;

public class GdMsgDetailPager extends SyncActivityModel {

    private GdMsgDetailPagerFragment fragment;

    @Override
    public int getViewResource() {
        return R.layout.activity_fragment;
    }

    @Override
    public void onReceiveSyncStatus(SyncServiceStatus syncServiceStatus, SyncStatusDialog syncStatusDialog) {
        super.onReceiveSyncStatus(syncServiceStatus, syncStatusDialog);
        syncStatusDialog.show(syncServiceStatus);
    }

    @Override
    public void onCreateActivity(Bundle savedInstanceState) {
        super.onCreateActivity(savedInstanceState);

        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (fragment != null)
            fragmentTransaction.remove(fragment);

        fragment = GdMsgDetailPagerFragment.newInstance();
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            fragment.setGdMsgId(bundle.getInt(GdMsgsListFragment.TAG_GD_MSG_ID));
            fragment.setTextFilter(bundle.getString(GdMsgsListFragment.TAG_TEXT_FILTER));
            fragment.setSenderFilter((GdMsgModel.Sender) bundle.getSerializable(GdMsgsListFragment.TAG_SENDER_FILTER));
        }

        fragmentTransaction.add(R.id.frame_content, fragment);
        fragmentTransaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.gdmsg, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new GCMHelper(this).checkPlayServices(this);
    }
}